home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Day Cry
/
Day Cry CD.bin
/
oh_towns
/
taropyon
/
splib
/
splib.lzh
/
PRG
/
LHX
/
UTIL.C
< prev
Wrap
C/C++ Source or Header
|
1992-12-08
|
5KB
|
251 lines
/***********************************************************
util.c -- utility routines
***********************************************************/
#include "lh386.h"
#include <fslib.h>
#include <stdio.h>
#ifndef __HIGHC__
# include <conio.h>
#endif
#include <io.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include "lh.h"
#include "intrface.h"
#include "errmes.h"
#ifdef __HIGHC__
# pragma On(Align_labels);
#endif
uchar pathdelim = '\\';
uchar swchar;
boolean copy_mode = FALSE;
/***************************************
convert path delimiter
****************************************
returns *filename
***************************************/
char *convdelim(char *path, char delim)
{
uchar c;
uchar *p;
int kflg = 0;
for (p = (uchar *) path; (c = *p) != 0; p++)
{
if (kflg)
{
kflg = 0;
} else if (iskanji(c))
{
kflg = 1;
} else if (c == '\\' || c == DELIM || c == DELIM2)
{
*p = delim;
path = (char *) p + 1;
}
}
return path;
}
char *getfilename(char *path)
{
char *q;
if ((q = strrchr(path, DELIM)) != NULL ||
(q = strchr(path, ':')) != NULL)
{
return q + 1;
} else
{
return path;
}
}
/*******************************
get back to parent directory
*******************************/
char *backpath(char *p)
{
char *q;
q = getfilename(p);
*q = '\0';
return q;
}
/*******************************
ask 'Y' or 'N'
*******************************/
int getyn(void)
{
int yn;
do
{
yn = LHX_getch();
yn = toupper(yn);
if ( yn == '\x03' )
error(CTRLBRK, NULL);
} while (yn != 'Y' && yn != 'N');
LHX_fprintf(stderr, "%c\n", yn);
return yn;
}
void getswchar(void)
{
union REGS regs;
regs.x.ax = 0x3700;
intdos(®s, ®s);
swchar = regs.h.dl;
if (swchar != '/')
{
pathdelim = '/';
}
}
time_t dos2unix(struct ftime * ft)
{
struct tm tm;
tm.tm_sec = ft->ft_tsec * 2;
tm.tm_min = ft->ft_min;
tm.tm_hour = ft->ft_hour;
tm.tm_mday = ft->ft_day;
tm.tm_mon = ft->ft_month - 1;
tm.tm_year = ft->ft_year + 80;
#ifndef __HIGHC__
tm.tm_isdst = timezone;
#else
tm.tm_isdst = -9 * 3600;
#endif
return mktime(&tm);
}
struct ftime *unix2dos(time_t utc)
{
static struct ftime ft;
struct tm *tm;
tm = localtime(&utc);
ft.ft_tsec = tm->tm_sec / 2;
ft.ft_min = tm->tm_min;
ft.ft_hour = tm->tm_hour;
ft.ft_day = tm->tm_mday;
ft.ft_month = tm->tm_mon + 1;
ft.ft_year = tm->tm_year - 80;
return &ft;
}
/*******************************
ratio * 1000
*******************************/
int ratio(ulong a, ulong b, int ord)
{
int i;
for (i = 0; i < ord && a < 0x19999999; i++)
{
a *= 10; /* while not overflow */
} /* upto 10^ord times */
for (; i < ord; i++)
{ /* the case of overflow */
b /= 10;
}
if (b == 0)
return 0; /* if diviser == 0 */
a += b / 2; /* for round up */
return (a / b); /* return (a * 1000 / b) */
}
#define BUFFERSIZE TEXT_SIZE
void copyfile(FILE * f1, FILE * f2, long size, int crc_flg)
{
ushort xsize;
copy_mode = TRUE;
crc = 0;
while (size > 0)
{
xsize = (size > BUFFERSIZE) ? BUFFERSIZE : size;
if (fread(text, 1, xsize, f1) != xsize)
{
fileerror(RDERR, f1);
}
if (f2)
{
#ifdef __LHA386__
if ( f2 == stdout || f2 == stderr )
{
size_t cnt;
char *s;
cnt = xsize;
s = (char *)text;
while ( cnt-- > 0 )
LHX_putc( *s++ );
} else
#endif
{ if (fwrite(text, 1, xsize, f2) != xsize)
fileerror(WTERR, f2);
}
}
if (crc_flg)
{
calccrc(text, xsize);
/* if (flg_n == 0) LHX_fputc('c', stderr); */
}
size -= xsize;
}
copy_mode = FALSE;
}
void *e_malloc(uint size)
{
void *p;
if ((p = malloc(size)) == NULL)
error(MEMOVRERR, NULL);
return p;
}
void *e_realloc(void *buf, int size)
{
void *p;
if ((p = realloc(buf, size)) == NULL)
error(MEMOVRERR, NULL);
return p;
}
FILE *myeopen(char *path, char *mode, char *errmes)
{
FILE *f;
/* LHX_printf("fopen(\x22%s\x22,\x22%s\x22)\n", path, mode ); */
f = FS_fopen(path, mode);
if (f == NULL && errmes != NULL)
error(errmes, path);
return f;
}
FILE *mywopen(char *path)
{
return myeopen(path, "wb", MKFILEERR);
}
FILE *myropen(char *path)
{
return myeopen(path, "rb", NOFILEERR);
}